#include "bankQueue.h"


bankQueue::bankQueue()
{
	init(3);
}

bankQueue::bankQueue(int workers)
{
	init(workers);
}

bankQueue::~bankQueue()
{
}

void bankQueue::init(int workers)
{
	srand(time(NULL));	
	numWorkers = workers;	
}

void bankQueue::simulate(int time)
{
	currentTime = 0;
	totalNumCustomers = 0;
	totalWaitTime = 0;
	workers.clear();
	int n = line.size();
	for (int i = 0; i < n; i++)
	{
		line.pop();
	}

	for (int i = 0; i < numWorkers; i++)
	{
		this->workers.push_back(worker());
	}

	while (currentTime < time)
	{
		//ny kund?
		{
			int n = rand() % 10;
			if (n < 9)
			{
				line.push(customer(currentTime));
			}
		}

		//lediga arbetare?
		for (int i = 0; i < workers.size(); i++)
		{
			
			if (workers[i].isFree())
			{
				if (line.size() > 0)
				{
					int n = (rand() % 7) + 2;
					workers[i].setServiceTime(n);					
					totalNumCustomers += 1;
					totalWaitTime += currentTime - line.front().getArrivedTime();
					line.pop();
				}
			}			
			else
			{
				workers[i].update();
			}
		}
		currentTime++;
	}
}

float bankQueue::getAvgWaitTime()
{
	return (float)totalWaitTime / (float)totalNumCustomers;
}